import React, { Component } from "react";

import PropTypes from "prop-types";

export default class TableControl extends Component {
  constructor() {
    super();
    this.state = {
      currentIndex: 0,
    };
  }
  render() {
    const { titles } = this.props;
    const { currentIndex } = this.state;

    return (
      <div className="tab-control">
        {titles.map((item, index) => {
          return (
            <div
              className={"tab-item " + (index === currentIndex ? "active" : "")}
              key={item}
              onClick={(e) => this.tabChange(index)}
            >
              {item}
            </div>
          );
        })}
      </div>
    );
  }
  tabChange = (index) => {
    this.props.itemClick(index);
    this.setState({
      currentIndex: index,
    });
  };
}

TableControl.propTypes = {
  titles: PropTypes.array.isRequired,
};
